#pragma once
#include <iostream>
#include "Common.hpp"

class Init
{
public:
    Init()
    {
        umask(0);
        int n = mkfifo(gpipeFile.c_str(), gmode);
        if(n < 0)
        {
            cerr<<"mkfifo error"<<endl;
            return ;
        }
        cout<<"mkfifo success"<<endl;
        sleep(1);
    }
    ~Init()
    {
        int n = unlink(gpipeFile.c_str());
        if(n < 0)
        {
            cerr<<"unlink error"<<endl;
        }
        cout<<"unlink success"<<endl;
    }
};

Init init;

class Server
{
public:
    Server()
    :_fd(gdefaultfd)
    {
    }
    bool OpenPipeForRead()
    {
        
        _fd = OpenPipe(gForRead);
        if(_fd < 0) return false;
        return true;
    }
    //读管道
    int RecPipe(string *out)
    {
        char buff[gsize];
        ssize_t n = ::read(_fd, buff, sizeof(buff) - 1);
        if(n > 0)
        {
            buff[n] = '\0';
            *out = buff;
        }
        return n;
    }
    void ClosePipe()
    {
        ClosePipeT(_fd);
    }
    ~Server()
    {}
private:
    pid_t _fd;
};